package ex.com.jm.food.action;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.DateProfitSummary;
import ex.com.jm.food.service.AdminDPSService;
import ex.com.jm.food.utils.AccountDate;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.ExcelUtil;
import ex.com.jm.food.utils.JsonUtils;

public class AdminDPSAction extends ActionSupport {

	
	private static final long serialVersionUID = 1L;

	private DateProfitSummary dps;
	private AdminDPSService adminDPSService;
	private String s_date;
	private String e_date;
	private String order;
	private String sort;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String date;
	private Map<String, String> dateMap;
	public DateProfitSummary getDps() {
		return dps;
	}
	public void setDps(DateProfitSummary dps) {
		this.dps = dps;
	}
	public AdminDPSService getAdminDPSService() {
		return adminDPSService;
	}
	public void setAdminDPSService(AdminDPSService adminDPSService) {
		this.adminDPSService = adminDPSService;
	}
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	
	public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String dpsList(){
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		dateMap=DateUtils.getDate();
		return "dpsList";
	}
	
	public void getDPSs(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		List<DateProfitSummary> dps=this.getAdminDPSService().getDPSs(offset, pageRow, date, s_date, e_date, sort, order);
		JSONArray jsondps=JSONArray.fromObject(dps);
		totalCount=this.getAdminDPSService().dpsCount(date, s_date, e_date);
	    JSONObject jo=JsonUtils.buildJson(jsondps, initPage, pageRow, totalCount);
			PrintWriter out=null;
			try {
				out = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.print(jo);
			out.flush();
			out.close(); 
	}
	
	public void DPSExcel(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("application/vnd.ms-excel");
		OutputStream out=null;
		try {
			out=response.getOutputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		List<DateProfitSummary> dps=this.getAdminDPSService().DPSExcel(date, s_date, e_date);
		String[] headers=new String[]{"时间","所属企业","售出菜品总量","企业收益","网站收益","收益率"};
		String[] columns=new String[]{"sDate","comName","totalNum","comTotalProfit","totalProfit","profitRate",};
		try {
			ExcelUtil.DPSExcel(dps, headers, columns, out);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void DPSReport(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		List<DateProfitSummary> dpss=this.getAdminDPSService().DPSReport(date, s_date, e_date);
		
		List<String> dateList=AccountDate.getEveryday(s_date, e_date);
		List<Double> totalProfit=new ArrayList<Double>();
		List<Double> comTotalProfit=new ArrayList<Double>();
		List<Integer> totalNum=new ArrayList<Integer>();
		List<Integer> profitRate=new ArrayList<Integer>();
		for (DateProfitSummary dps : dpss) {
			totalProfit.add(dps.getTotalProfit());
			comTotalProfit.add(dps.getComTotalProfit());
			totalNum.add(dps.getTotalNum());
			profitRate.add(dps.getProfitRate());
		}
		JSONArray jototalprofit=JSONArray.fromObject(totalProfit);
		JSONArray jocomTotalProfit=JSONArray.fromObject(jototalprofit);
		JSONArray jototalNum=JSONArray.fromObject(totalNum);
		JSONArray joprofitRate=JSONArray.fromObject(profitRate);
		JSONArray jodate=JSONArray.fromObject(dateList);
		JSONObject jo=new JSONObject();
		jo.put("totalProfit", jototalprofit);
		jo.put("comTotalProfit", jocomTotalProfit);
		jo.put("totalNum", jototalNum);
		jo.put("profitRate", joprofitRate);
		jo.put("date", jodate);
		System.out.println(jo);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
}
